﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using BENBUN.Properties;
using BENBUN.WIW;

namespace BENBUN.Utils
{
    class Session
    {
        public static string getToken(string version)
        {
            XmlDocument doc = new XmlDocument();

            string apiURL = Settings.Settings.URLBaseAPI + "user.php";
            apiURL = apiURL + "?u=" + Settings.Settings.ForumUsername
                + "&p=" + MD5Tool.GetMD5Hash(Settings.Settings.ForumPassword)
                + "&sw=" + version
                + "&mode=token";

            try
            {
                doc.Load(apiURL);

                XmlNodeList nodes = doc.SelectNodes("/wiw/validate");

                string tempValid = "";
                string tempToken = "";

                foreach (XmlNode node in nodes)
                {
                    tempValid = node.Attributes["user"].Value.ToString();
                    tempToken = node.Attributes["token"].Value.ToString();
                }

                if (tempValid == "true")
                {
                    return tempToken;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception)
            {

                return null;
            }



        }
    }
}
